<?php

namespace app\admin\controller\track;

use app\common\controller\Backend;

class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $date = $this->request->post('date', '');
        $data = $this->getCodeStatisticsData($date);
        $statistics = ['columns' => array_keys($data), 'data' => array_values($data)];
        //总防伪码数
        $count_code = \app\admin\model\track\Code::where('switch',1)->count();
        //未查询数量
        $nf_code = \app\admin\model\track\Code::where('find_num',0)->count();
        //今日查询次数
        $today_code_history = \app\admin\model\track\History::whereTime('search_time', 'today')->count();
        //今日首次查询次数
        $today_first_code_history = \app\admin\model\track\History::where('status',1)->whereTime('search_time', 'today')->count();
        //总查询次数
        $count_code_history = \app\admin\model\track\History::count();
        //总产品数
        $count_product = \app\admin\model\track\Product::where('switch',1)->count();
        //总代理商数
        $count_agent = \app\admin\model\track\Agent::where('switch',1)->count();
        //总分销商数
        $count_dealer = \app\admin\model\track\Dealer::count();
        $this->view->assign([
            'count_code'        => $count_code,
            'nf_code'          =>$nf_code,
            'today_code_history'       => $today_code_history,
            'today_first_code_history'       => $today_first_code_history,
            'count_code_history' => $count_code_history,
            'count_product'           =>$count_product,
            'count_agent'          => $count_agent,
            'count_dealer'       => $count_dealer,

        ]);
        if ($this->request->isPost()) {
            $data = [
                'statistics'=>$statistics,
            ];
            $this->success('', '', $data);
        }
        $this->assignconfig('statistics', $statistics);
        return $this->view->fetch();
    }

    /**
     * 获取扫码统计数据
     * @param string $date
     * @return array
     */
    protected function getCodeStatisticsData($date = '')
    {
        if ($date) {
            list($start, $end) = explode(' - ', $date);

            $starttime = strtotime($start);
            $endtime = strtotime($end);
        } else {
            $starttime = \fast\Date::unixtime('day', 0, 'begin');
            $endtime = \fast\Date::unixtime('day', 0, 'end');
        }
        $totalseconds = $endtime - $starttime;

        $format = '%Y-%m-%d';
        if ($totalseconds > 86400 * 30 * 2) {
            $format = '%Y-%m';
        } else if ($totalseconds > 86400) {
            $format = '%Y-%m-%d';
        } else {
            $format = '%H:00';
        }
        $orderlist =\app\admin\model\track\History::where('search_time', 'between time', [$starttime, $endtime])
            ->field('search_time, COUNT(*) AS nums, MIN(search_time) AS min_search_time, MAX(search_time) AS max_search_time, 
            DATE_FORMAT(FROM_UNIXTIME(search_time), "' . $format . '") AS comment_date')
            ->group('comment_date')
            ->select();
        if ($totalseconds > 84600 * 30 * 2) {
            $starttime = strtotime('last month', $starttime);
            while (($starttime = strtotime('next month', $starttime)) <= $endtime) {
                $column[] = date('Y-m', $starttime);
            }
        } else if ($totalseconds > 86400) {
            for ($time = $starttime; $time <= $endtime;) {
                $column[] = date("Y-m-d", $time);
                $time += 86400;
            }
        } else {
            for ($time = $starttime; $time <= $endtime;) {
                $column[] = date("H:00", $time);
                $time += 3600;
            }
        }
        $list = array_fill_keys($column, 0);
        foreach ($orderlist as $k => $v) {
            $list[$v['comment_date']] = $v['nums'];
        }
        return $list;
    }
}
